package com.qfedu.provider.listener;


import com.qfedu.config.RocketmqConfig;
import com.qfedu.params.AddCollection;
import com.qfedu.provider.entity.DbDynamics;
import com.qfedu.provider.entity.DbVideo;
import com.qfedu.provider.mapper.DbDynamicsMapper;
import com.qfedu.provider.mapper.DbVideoMapper;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component
@RequiredArgsConstructor
@Slf4j
@RocketMQMessageListener(topic = RocketmqConfig.TOPIC_COLLECTION_NUM_ADD,consumerGroup = "collection-num-add-group")
public class AddCollectionNumListener implements RocketMQListener<AddCollection> {
    private final DbVideoMapper dbVideoMapper;
    private final DbDynamicsMapper dbDynamicsMapper;

    @Transactional
    @Override
    public void onMessage(AddCollection addCollection) {
        log.info("开始添加作品的总的收藏数");
        //判断作品所属类型
        if(addCollection.getProductionTypeId()==1){
            //图文混排 收藏总数减一或加一
            //1.先查询出作品
            DbDynamics dbDynamics = dbDynamicsMapper.selectById(addCollection.getProductionId());
            //2.修改收藏总数
            System.out.println(dbDynamics);
            dbDynamics.setCollectionNumber(dbDynamics.getCollectionNumber()+1);
            dbDynamics.setUpdateTime(new Date());
            dbDynamicsMapper.updateById(dbDynamics);
        }else {
            //视频
            DbVideo dbVideo = dbVideoMapper.selectById(addCollection.getProductionId());
            dbVideo.setCollectionNumber(dbVideo.getCollectionNumber()+1);
            dbVideo.setUpdateTime(new Date());
            dbVideoMapper.updateById(dbVideo);
        }
        log.info("添加作品的总的收藏数成功");

    }
}
